package com.leetcode.algorithm.y22.m07.w5;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 1331. 数组序号转换
 * 
 * https://leetcode.cn/problems/rank-transform-of-an-array/
 * 
 * @author jie.deng
 *
 */
class Question1331Solution01 {

	public int[] arrayRankTransform(int[] arr) {
		int len = arr.length;
		int[] ans = new int[len];
		if (len == 0) {
			return ans;
		}

		int[] arrClone = Arrays.copyOf(arr, len);
		Arrays.sort(arrClone);

		Map<Integer, Integer> map = new HashMap<>();
		int seq = 1;
		map.put(arrClone[0], seq++);
		
		for (int i = 1; i < len; i++) {
			if (arrClone[i] != arrClone[i - 1]) {
				map.put(arrClone[i], seq++);
			}
		}
		for (int i = 0; i < len; i++) {
			ans[i] = map.get(arr[i]).intValue();
		}
		return ans;
	}

}